{include file="public/header" /}
<script type="text/javascript"
        src="{$Think.HTTP_TYPE}api.map.baidu.com/api?v=2.0&ak=22bb7221fc279a484895afcc6a0bb33a"></script>
<link rel="stylesheet" href="{$Think.HOME_SITE_ROOT}/css/common.css">
<script>
    var local;
    var map;
    var lst_name = '';

    function change_map() {
        if ($("#area_ select:eq(0)").length > 0 && $("#area_ select:eq(0) option:selected").val() != '') {
            var name = $("#area_ select:eq(0) option:selected").text();
        }
        if ($("#area_ select:eq(1)").length > 0 && $("#area_ select:eq(1) option:selected").val() != '') {
            var name = $("#area_ select:eq(1) option:selected").text();
        }
        if ($("#area_ select:eq(2)").length > 0 && $("#area_ select:eq(2) option:selected").val() != '') {
            var name = $("#area_ select:eq(2) option:selected").text();
        }
        if (name != '' && lst_name != name) {
            lst_name = name;
            map.setCurrentCity(name);
            map.centerAndZoom(name, 16);
            map.clearOverlays();
            local.search(name);
        }

    }

    $(function () {
        map = new BMap.Map("allmap");
        var geolocation = new BMap.Geolocation();
        geolocation.getCurrentPosition(function (r) {
            if (this.getStatus() == BMAP_STATUS_SUCCESS) {
                var lng = r.point.lng;
                var lat = r.point.lat;
                var point = new BMap.Point(lng, lat);
                map.centerAndZoom(point, 16);
                map.addControl(new BMap.NavigationControl());
                map.enableScrollWheelZoom();
                var marker = new BMap.Marker(point);  // 创建标注
                map.addOverlay(marker);              // 将标注添加到地图中
                marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
                document.getElementById("longitude").value = lng;
                document.getElementById("latitude").value = lat;

            } else {
                layer.msg('failed' + this.getStatus());
            }
        }, {enableHighAccuracy: true})

        var options = {
            onSearchComplete: function (results) {
                // 判断状态是否正确
                if (local.getStatus() == BMAP_STATUS_SUCCESS) {
                    if (results.getCurrentNumPois() > 0) {

                        map.clearOverlays();  //清除标注  或者可以把market 放入数组
                        var point = new BMap.Point(results.getPoi(0).point.lng, results.getPoi(0).point.lat);
                        var marker = new BMap.Marker(point);
                        map.centerAndZoom(point, 16);
                        map.addOverlay(marker);
                        marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画

                        document.getElementById("longitude").value = results.getPoi(0).point.lng;
                        document.getElementById("latitude").value = results.getPoi(0).point.lat;

                    }
                }
            }
        };
        local = new BMap.LocalSearch(map, options);
        map.addEventListener("click", function (e) {
//                    alert(e.point.lng + ", " + e.point.lat);
            map.clearOverlays();  //清除标注  或者可以把market 放入数组
            var point = new BMap.Point(e.point.lng, e.point.lat);
            var marker = new BMap.Marker(point);
            map.addOverlay(marker);
            marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画

            document.getElementById("longitude").value = e.point.lng;
            document.getElementById("latitude").value = e.point.lat;
        });
    })
</script>


<style>
    ul > li {
        width: auto !important;
    }

</style>


<div class="page">
    <div class="fixed-bar">
        <div class="item-title">
            <div class="subject">
                <h3>修改二手商品</h3>
            </div>
            {include file="public/admin_items" /}
        </div>
    </div>

    <form id="add_form" enctype="multipart/form-data" method="post" action="{url('Twogoods/edit_goods')}"
          onSubmit="return confirm();">
        <table class="ds-default-table">
            <tbody>

            <tr class="noborder">
                <td colspan="2" class="required"><label class="validation" for="gc_name">商品名称:</label>
                    <input type="text" value="{$goods_info.goods_name}" name="goods_name" id="gc_name" maxlength="1000"
                           class="txt" style="width: 200px; margin-left: 56px;"><span></span></td>
                <td class="vatop tips"></td>
            </tr>
            <tr>
                <td colspan="2" class="required"><label>商品分类:</label>
                    <?php if(!empty($goods_info['gc_name'])){  ?>
                    <div id="gcategorys" style="margin-left: 130px;margin-top:-20px;">
                        {$goods_info.gc_name} <input type="button" value="编辑" id="gc_edit">
                    </div>

                    <div id="gcategory" style="margin-left: 130px;margin-top:-20px; display:none;">
                        <input type="hidden" name="class_id" value="1000000" class="mls_id" id="class_id"/>
                        <input type="hidden" name="class_name" value="{$type.class_name|default=''}" class="mls_name"/>
                        {if $type.class_id}
                        <span>{$type.class_name|default=''}</span>
                        <input type="button" value="{$Think.lang.ds_edit}" class="edit_gcategory"/>
                        {/if}
                        <select {if $type.class_id}style="display:none" {/if}>
                        <option value="0">请选择</option>
                        {foreach name="gc_list" item="gc"}
                        <option value="{$gc.gc_id}">{$gc.gc_name}</option>
                        {/foreach}
                        </select>
                    </div>
                    <?php }else{ ?>


                    <?php }?>
                </td>
            </tr>
            <tr>
                <td colspan="2" class="required"><label class="validation">商品图片:</label>
                    <input type="file" id="upload_img" url="{$Think.UPLOAD_SITE_URL}" style="margin-left: 28px;">
                </td>
                <td class="vatop tips">
                    <span data_id="-1"></span>
                    {foreach name="goods_images" item="val" key="k" }

                    <span data_id='{$k}'>
                  <img src="{$val.goodsimage_url}" width="120" height="120"/>
                  <input type='hidden' name="pic[{$k}]" value="{$val.goodsimage_url}"/>
                  <input type='button' value='删除' class='del_img' data_id="{$k}" data_url="{$val.goodsimage_url}"/>
                </span>
                    {/foreach}
                </td>
            </tr>
            <tr>
                <td colspan="2" class="required"><label>商品品牌:</label>
                    <div style='margin-left: 131px;margin-top: -18px;' id="ajax_brand" class="dssc-brand-select w180">
                        <div class="selection">
                            <input name="b_name" id="b_name" value="{$goods_info.brand_name|default=''}" type="text"
                                   class="txt w180" readonly="readonly"/>
                            <input type="hidden" name="b_id" id="b_id" value="{$goods_info.brand_id|default=''}"/>
                        </div>
                        <div class="dssc-brand-select-container">
                            <div class="brand-index"
                                 style="width: 210px;padding-bottom: 10px;margin: 6px auto;border-bottom: dotted 1px #CCC;"
                                 data-url="{:url('Common/ajax_get_brand')}">
                                <div class="letter" dstype="letter">
                                    <ul style="width: 100%;">
                                        <li style="width: auto;"><a href="javascript:void(0);"
                                                                    data-letter="all">所有品牌</a></li>
                                        <li style="width: auto;"><a href="javascript:void(0);" data-letter="A">A</a>
                                        </li>
                                        <li style="width: auto;"><a href="javascript:void(0);" data-letter="B">B</a>
                                        </li>
                                        <li style="width: auto;"><a href="javascript:void(0);" data-letter="C">C</a>
                                        </li>
                                        <li style="width: auto;"><a href="javascript:void(0);" data-letter="D">D</a>
                                        </li>
                                        <li style="width: auto;"><a href="javascript:void(0);" data-letter="E">E</a>
                                        </li>
                                        <li style="width: auto;"><a href="javascript:void(0);" data-letter="F">F</a>
                                        </li>
                                        <li style="width: auto;"><a href="javascript:void(0);" data-letter="G">G</a>
                                        </li>
                                        <li style="width: auto;"><a href="javascript:void(0);" data-letter="H">H</a>
                                        </li>
                                        <li style="width: auto;"><a href="javascript:void(0);" data-letter="I">I</a>
                                        </li>
                                        <li style="width: auto;"><a href="javascript:void(0);" data-letter="J">J</a>
                                        </li>
                                        <li style="width: auto;"><a href="javascript:void(0);" data-letter="K">K</a>
                                        </li>
                                        <li style="width: auto;"><a href="javascript:void(0);" data-letter="L">L</a>
                                        </li>
                                        <li style="width: auto;"><a href="javascript:void(0);" data-letter="M">M</a>
                                        </li>
                                        <li style="width: auto;"><a href="javascript:void(0);" data-letter="N">N</a>
                                        </li>
                                        <li style="width: auto;"><a href="javascript:void(0);" data-letter="O">O</a>
                                        </li>
                                        <li style="width: auto;"><a href="javascript:void(0);" data-letter="P">P</a>
                                        </li>
                                        <li style="width: auto;"><a href="javascript:void(0);" data-letter="Q">Q</a>
                                        </li>
                                        <li style="width: auto;"><a href="javascript:void(0);" data-letter="R">R</a>
                                        </li>
                                        <li style="width: auto;"><a href="javascript:void(0);" data-letter="S">S</a>
                                        </li>
                                        <li style="width: auto;"><a href="javascript:void(0);" data-letter="T">T</a>
                                        </li>
                                        <li style="width: auto;"><a href="javascript:void(0);" data-letter="U">U</a>
                                        </li>
                                        <li style="width: auto;"><a href="javascript:void(0);" data-letter="V">V</a>
                                        </li>
                                        <li style="width: auto;"><a href="javascript:void(0);" data-letter="W">W</a>
                                        </li>
                                        <li style="width: auto;"><a href="javascript:void(0);" data-letter="X">X</a>
                                        </li>
                                        <li style="width: auto;"><a href="javascript:void(0);" data-letter="Y">Y</a>
                                        </li>
                                        <li style="width: auto;"><a href="javascript:void(0);" data-letter="Z">Z</a>
                                        </li>
                                        <li style="width: auto;"><a href="javascript:void(0);" data-letter="0-9">其他</a>
                                        </li>
                                    </ul>
                                </div>
                                <div class="search" dstype="search"><input name="search_brand_keyword"
                                                                           id="search_brand_keyword" type="text"
                                                                           class="text"/><a href="javascript:void(0);"
                                                                                            class="dssc-btn-mini"
                                                                                            style="vertical-align: top;">Go</a>
                                </div>
                            </div>
                            <div class="brand-list" dstype="brandList">
                                <ul dstype="brand_list">
                                    {notempty name="brand_list"}
                                    {foreach name="brand_list" item="val"}
                                    <li data-id='{$val.brand_id}' data-name='{$val.brand_name}'>
                                        <em>{$val.brand_initial}</em>{$val.brand_name}
                                    </li>
                                    {/foreach}
                                    {/notempty}
                                </ul>
                            </div>
                            <div class="no-result" dstype="noBrandList" style="display: none;">
                                {$Think.lang.no_brand_result}
                            </div>
                        </div>
                    </div>
                </td>
                <!--  <td class="vatop tips">{$Think.lang.goods_class_add_sup_class_notice}</td> -->
            </tr>

            <tr>
                <td colspan="2" class="required"><label class="validation">数量:</label><input class="w60" type="text"
                                                                                             name="count"
                                                                                             value="{$goods_info.goods_storage}"
                                                                                             style="margin-left: 86px;"><span></span>
                </td>
                <!-- <td class="vatop tips">{$Think.lang.commis_rate_tips}</td> -->
            </tr>
            <tr>
                <td colspan="2" class="required"><label class="validation">售价:</label><input class="w60" type="text"
                                                                                             name="price"
                                                                                             value="{$goods_info.goods_price}"
                                                                                             style="margin-left: 86px;"><span></span>
                </td>
                <td class="vatop tips"></td>
            </tr>
            <tr>
                <td colspan="2" class="required"><label class="validation">所在地:</label>
                    <div id="area_s" style="margin-left: 131px;margin-top:-20px;">
                        {$goods_info.two_goods_address} <input type="button" value="编辑" id="address_edit">
                        <input type="hidden" id="_area_4" value="1">
                    </div>
                    <p id="area_" style="margin-left: 134px; margin-top: -19px;  display:none;">
                        <input type="hidden" value="" name="company_address" id="company_address">
                        <input type="hidden" value="" name="province_id" id="_area_1">
                        <input type="hidden" value="" name="city_id" id="_area_2">
                        <input type="hidden" value="1" name="district_id" id="_area_3">
                        <span></span></p></td>

            </tr>
            <tr>
                <td colspan="2" class="required"><label class="validation">联系方式:</label><input id="commis_rate"
                                                                                               class="w360" type="text"
                                                                                               name="phone"
                                                                                               value="{$goods_info.two_phone}"
                                                                                               style="margin-left: 58px;"><span></span>
                </td>
                <td class="vatop tips"></td>
            </tr>
            <tr>
                <td colspan="2" class="required"><label class="validation">说明:</label>
                    <textarea name="content" id="" cols="30" rows="10" style="margin-left: 81px;">{$goods_info.goods_body}</textarea><span></span>
                </td>
                <td class="vatop tips"></td>
            </tr>
            <tr>
                <td class="required w120">是否可议价</td>
                <td>
                    <div class="onoff">
                        <label for="sms_login_show1" class="cb-enable {if condition="$goods_info.two_is_price eq 1"}selected{/if}">{$Think.lang.ds_yes}</label>
                        <label for="sms_login_show0" class="cb-disable {if condition="$goods_info.two_is_price eq 0"}selected{/if}">{$Think.lang.ds_no}</label>
                        <input id="sms_login_show1" name="two_is_price" value="1" type="radio" {if condition="$goods_info.two_is_price eq 1"} checked="checked"{/if}>
                        <input id="sms_login_show0" name="two_is_price" value="0" type="radio" {if condition="$goods_info.two_is_price eq 0"} checked="checked"{/if}>
                    </div>
                </td>
            </tr>

            </tbody>
            <tfoot>
            <tr>
                <input type='hidden' name='commonid' value="{$goods_info.goods_commonid}"/>
                <td colspan="2"><input class="btn" type="submit" value="{$Think.lang.ds_submit}"
                                       style='width:100px; margin-left:70px;'/>
                </td>
            </tr>
            </tfoot>
        </table>
    </form>
</div>
<script type="text/javascript" src="{$Think.PLUGINS_SITE_ROOT}/mlselection.js" charset="utf-8"></script>
<script type="text/javascript" src="{$Think.PLUGINS_SITE_ROOT}/jquery.mousewheel.js"></script>
<script>
    //按钮先执行验证再提交表单
    http_name = "{$http_name}";

    $(document).ready(function () {

        /*编辑分类*/
        $('#gc_edit').click(function () {
            $('#gcategorys').remove();
            $('#gcategory').show();
        });

        /*编辑所在地*/
        $('#address_edit').click(function () {
            $('#area_s').remove();
            $('#area_').show();
        });


        /* AJAX选择品牌 */
        $("#ajax_brand").brandinit();

        $('#dssubmit').click(function () {
            $('#formSearch').submit();
        });

        $('#dssubmit').click(function () {
            $('#formSearch').submit();
        });
        $('#company_address').ds_region();
    });
    $(function () {
        // 类型搜索
        $(document).off('change', "#gcategory > select").on("change", "#gcategory > select", function () {
            type_scroll($(this));
        });
        var typeScroll = 0;

        function type_scroll(o) {
            var id = o.val();
            if (!$('#type_dt_' + id).is('dt')) {
                return false;
            }
            $('#type_div').scrollTop(-typeScroll);
            var sp_top = $('#type_dt_' + id).offset().top;
            var div_top = $('#type_div').offset().top;
            $('#type_div').scrollTop(sp_top - div_top);
            typeScroll = sp_top - div_top;
        }

        gcategoryInit('gcategory');

        //图片删除
        $(document).on('click', '.del_img', function () {
            /* var lastId = $(this).parent().parent().parent().find('tr').last().attr('data-id');
             html+="<input type='hidden' name='area_id["+id+"]' value='"+chk_value+"'>";*/
            var data_id = $(this).attr('data_id');
            var data_url = $(this).attr('data_url');
            var html = "<input type='hidden' name='del_pic[" + data_id + "]' value='" + data_url + "' />";
            $(this).parent().remove();
        });


        //图片上传并展示
        $(document).on('change', '#upload_img', function () {
            var files = document.getElementById('upload_img').files[0];
            var lastId = $(this).parent().next().find('span').last().attr('data_id');
            var lastid = parseInt(lastId) + 1;
            var url = $(this).attr('url');
            var obj = $(this).parent().next();
            var formData = new FormData();
            formData.append('file', files);
            $.ajax({
                //请求方式
                type: "POST",
                //请求地址
                url: "ajax_upload",
                data: formData,
                cache: false,  //默认是true，但是一般不做缓存
                processData: false, //用于对data参数进行序列化处理，这里必须false；如果是true，就会将FormData转换为String类型
                contentType: false,  //一些文件上传http协议的关系，自行百度，如果上传的有文件，那么只能设置为false
                //请求成功
                success: function (result) {
                    if (result.err == 0) {
                        layer.msg(result.msg);
                        $('#upload_img').val('');
                        var html = "<span data_id='" + lastid + "'><img src='" + http_name + '/' + result.data + "' width='120' height='120'/><input type='hidden' name='pic[" + lastid + "]'  value='" + result.data + "'  /><input type='button' value='删除' class='del_img' data_id='" + lastid + "' data_url='" + result.data + "'/></span>";
                        obj.append(html);
                    } else {
                        $('#upload_img').val('');
                        alert('文件信息错误,请重新上传');
                        return false;
                    }


                },
                //请求失败，包含具体的错误信息
                error: function (e) {
                    console.log(e.status);
                    console.log(e.responseText);
                }
            });


            /* var lastId = $(this).parent().parent().parent().find('tr').last().attr('data-id');
             html+="<input type='hidden' name='area_id["+id+"]' value='"+chk_value+"'>";*/
            /*  var data_id = $(this).attr('data_id');
              var data_url = $(this).attr('data_url');
              var  html="<input type='hidden' name='del_pic["+data_id+"]' value='"+data_url+"' />";
              $(this).parent().html(html);   */
            /* */

        });

    });

    jQuery.validator.addMethod("checkFCodePrefix", function (value, element) {
        return this.optional(element) || /^[a-zA-Z]+$/.test(value);
    }, '<i class="iconfont">&#xe64c;</i>{$Think.lang.prompt_information1}');
    $("#add_form").validate({
        errorPlacement: function (error, element) {
            $(element).next('span').append(error);
        },
        rules: {
            content: {
                required: true
            },
            goods_name: {
                required: true
            },
            count: {
                required: true,
                digits: true,
                min: 1
            },
            price: {
                required: true,
                number: true,
                min: 0.01
            },
            phone: {
                required: true,
                minlength: 11,
                maxlength: 11,
            }
        },
        messages: {
            content: {
                required: "请填写描述"
            },
            goods_name: {
                required: "不能为空"
            },
            count: {
                required: "不能为空",
                digits: "请设置大于0的整数",
                min: '请设置大于0的整数'
            },
            price: {
                required: "不能为空",
                number: "请设置大于0的整数",
                min: '请设置大于0的整数'
            },
            phone: {
                required: "请输入手机号",
                minlength: "不能小于11个字符",
                maxlength: "不能大于11个字符",
            }
        }
    });


    function confirm() {
        var  _area_3= $('#_area_3').val();
        var _area_1 = $('#_area_1').val();
        var class_id = $('#class_id').val();
        var lastId = $('#upload_img').parent().next().find('span').last().attr('data_id');
        if (_area_3 == ''&&_area_1!='') {
            layer.msg('请选择所在地');
            return false;
        }
        var ress = $("#gcategory").is(":hidden");
        if (!ress&&class_id == '1000000') {
            layer.msg('请选择分类');
            return false;
        }
        var res = $("#area_").is(":hidden");
        if (!res&&_area_1=='') {
            layer.msg('请选择所在地');
            return false;
        }
        if (lastId == '-1') {
            layer.msg('请上传图片');
            return false;
        }
        var res = $('form').valid();
        if (res) {
            var url = $('form').attr('action');
            //alert(url);return false;
            // alert(url);return false;
            $.ajax({
                type: "POST",
                url: url,
                data: $('#add_form').serialize(),// 你的formid

                error: function (request) {
                    alert("Connection error");
                },
                success: function (data) {
                    if (data.code == 0) {

                        layer.msg(data.msg);
                        location.href = data.url;
                        return false;
                    } else {
                        layer.msg(data.msg);
                        location.href = data.url;
                        return false;

                    }
                }
            });return false;
        } else {
            layer.msg('操作失败');
            return false;
        }
    }

</script> 
